﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lyon.Utility
{
    /// <summary>
    /// 自定义格式（例如：格式模板"前缀{项1}分隔符{项2}后缀"对应的格式"前缀项1分隔符项2后缀"。）
    /// </summary>
    public class CustomFormat
    {
        string template;
        List<string> splits = new List<string>();
        List<string> keys = new List<string>();
        string[] values;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="formatTemplate">格式模板</param>
        public CustomFormat(string formatTemplate)
        {
            this.template = formatTemplate;
            InitTemplateFormat(formatTemplate, false);
            values = new string[keys.Count];
        }
        private void InitTemplateFormat(string value, bool needPrefix)
        {
            int startIndex = value.IndexOf("{");
            if (startIndex < 0)
            {
                splits.Add(value);
                return;
            }
            if (needPrefix && startIndex == 0)
                return;

            int endIndex = value.IndexOf("}");
            if (endIndex < 0)
            {
                throw new ArgumentException();
                //return;
            }

            string prefix = value.Remove(startIndex);
            string key = value.Substring(startIndex + 1, endIndex - startIndex - 1);
            string nextValue = value.Substring(endIndex + 1, value.Length - endIndex - 1);

            if (String.IsNullOrEmpty(key) || keys.Contains(key))
                throw new ArgumentException();

            splits.Add(prefix);
            keys.Add(key);
            if (String.IsNullOrEmpty(nextValue))
                splits.Add(nextValue);
            else
                InitTemplateFormat(nextValue, true);
        }
        /// <summary>
        /// 解析格式
        /// </summary>
        /// <param name="format"></param>
        public void UnFormat(string format)
        {
            Array.Clear(values, 0, values.Length);
            if (String.IsNullOrEmpty(splits[0]))
                UnFormat(format, 0);
            else
            {
                if (format.StartsWith(splits[0]))
                    UnFormat(format.Remove(0, splits[0].Length), 0);
            }
        }
        private void UnFormat(string format, int keyIndex)
        {
            string split = splits[keyIndex + 1];
            if (String.IsNullOrEmpty(split))
            {
                values[keyIndex] = format;
                return;
            }

            int index = format.IndexOf(split);
            if (index < 0)
            {
                values[keyIndex] = format;
                return;
            }

            values[keyIndex] = format.Remove(index);

            string nextformat = format.Remove(0, index + split.Length);
            if (!String.IsNullOrEmpty(nextformat))
                UnFormat(nextformat, keyIndex + 1);

        }
        /// <summary>
        /// 生成格式
        /// </summary>
        /// <returns></returns>
        public string Format()
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < keys.Count; i++)
            {
                builder.Append(splits[i]);
                builder.Append(values[i]);
            }
            builder.Append(splits[splits.Count - 1]);
            return builder.ToString();
        }
        //public string Format(params string[] values)
        //{
        //    if (values.Length > this.values.Length)
        //        throw new ArgumentException();
        //    for (int i = 0; i < values.Length; i++)
        //        this.values[i] = values[i];
        //    return Format();
        //}


        /// <summary>
        /// 获取格式项集合
        /// </summary>
        public string[] Keys
        {
            get
            {
                return keys.ToArray();
            }
        }
        /// <summary>
        /// 设置格式项值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValue(string key)
        {
            int index = keys.IndexOf(key);
            return values[index];
        }
        /// <summary>
        /// 获取格式项值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetValue(string key, string value)
        {
            int index = keys.IndexOf(key);
            values[index] = value;
        }
    }
}
